package com.yunxun.coinmessage.service;

import com.yunxun.coinmessage.dao.*;
import com.yunxun.coinmessage.entity.*;
import com.yunxun.coinmessage.templatedata.mesSorurceTemplateData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class MesSourceService {

    /**
     * 渠道业务逻辑处理
     * mesResourceAdd()添加渠道方式
     */
    @Autowired
    MesSourceDao mesResourceDao;
    @Autowired
    ClassifiedPacketDao classifiedPacketDao;//注入分类dao
    @Autowired
    TypeDictDao typeDictDao;

    private mesSorurceTemplateData mesSorurceTemplateData;
    private MesSource mesSource;
    private ClassifiedPacket classifiedPacket;
    private TypeDict typeDict;

    //查询全部渠道记录,并组装要返回的数据
    public List<mesSorurceTemplateData> queryAll() {
        List<mesSorurceTemplateData> mesSorurceTemplateDataList = new ArrayList<mesSorurceTemplateData>();//初始化要要返回的模型list

        List<MesSource> mesSourceList = mesResourceDao.queryAll();
        for (MesSource mesSource : mesSourceList) {//遍历查询出的渠道
            mesSorurceTemplateData = new mesSorurceTemplateData();//初始化模型
            mesSorurceTemplateData.setClassifiedPacket(classifiedPacketDao.findById(mesSource.getCpid()));//将所属分类设置进model中
            mesSorurceTemplateData.setTypeDict(typeDictDao.queryById(mesSource.getTypeId()));//将类型设置model中
            mesSorurceTemplateData.setMesSource(mesSource);
            mesSorurceTemplateDataList.add(mesSorurceTemplateData);
        }
        return mesSorurceTemplateDataList;
    }
    public  Integer queryTotal(){
        return mesResourceDao.queryTotal();
    }
    public void addMesSource(MesSource mesSource){

        mesResourceDao.addMesSource(mesSource);
    }
    public Integer deleteById(Integer sid)
    {
        return  mesResourceDao.deleteById(sid);
    }

    public MesSource queryById(Integer sid)
    {
        return mesResourceDao.queryByid(sid);
    }
    public  void updateById(MesSource mesSource)
    {
        mesResourceDao.updateById(mesSource);
    }
    public List<MesSource> queryByTypeId(Integer typeId){
        return  mesResourceDao.queryByTypeId(typeId);
    }

    /**
     * 按分类查询渠道
     * @param cpid
     * @return
     */

    public  List<MesSource> queryMesSourceByClassifiedPacket(Integer cpid){
        return mesResourceDao.queryByClassifiedPacket(cpid);
    }
    public MesSource queryByName(String sname){
        return  mesResourceDao.queryByName(sname);

    }
}
